package com.example.water.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.water.pojo.RespBean;
import com.example.water.pojo.StSitinfoB;
import com.example.water.pojoDTO.StSitinfoBDTO;
import com.example.water.service.StRsvrRService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.sun.tools.jconsole.JConsoleContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author Shunrai
 * @create 2023/11/6 9:56
 * @Version 1.0
 * @Description
 */
@Tag(name = "实时水情-水库")
@RestController
@RequestMapping("/RsvrR")
public class StRsvrRController {
    @Autowired
    private StRsvrRService stRsvrRService;

    @Operation(summary = "获取所有水库数据")
    @GetMapping("/getAll")
    public RespBean<StSitinfoBDTO> getAllReservoir(){
        Integer count = stRsvrRService.getAllCount();
        List<StSitinfoB> rPage = stRsvrRService.getStRsvrRPage(count, 1);
        StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(rPage,count);
        return RespBean.success("查询成功",stSitinfoBDTO);
    }

    @Operation(summary = "分页获取水库数据")
    @GetMapping("/page")
    public RespBean<StSitinfoBDTO> getRsvrRPage(@RequestParam(defaultValue = "7" ,required = false)Integer pageSize,
                                                @RequestParam(defaultValue = "1",required = false)Integer currentPage){
        List<StSitinfoB> rPage = stRsvrRService.getStRsvrRPage(pageSize, currentPage);
        Integer count = stRsvrRService.getAllCount();
        StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(rPage,count);
        return RespBean.success("查询成功",stSitinfoBDTO) ;
    }

    @Operation(summary = "站点名搜索")
    @GetMapping("/search")
    public RespBean<StSitinfoBDTO> searchReservoir(@Parameter(description = "站点名称",required = true)String stName,
                                                   @RequestParam(defaultValue = "7" ,required = false)Integer pageSize,
                                                   @RequestParam(defaultValue = "1",required = false)Integer currentPage){
       String stName1 = stName.trim();
        if(!"".equals(stName1)){
            //获取所有数据
            Integer count = stRsvrRService.getAllCount();
            List<StSitinfoB> rPage = stRsvrRService.getStRsvrRPage(count, 1);
            List<StSitinfoB> stream = rPage.stream().filter(v -> Objects.equals(v.getStname(), stName1)).collect(Collectors.toList());
            List<StSitinfoB> resultList;
            if (stream.size() > (currentPage-1)*pageSize) {
                // 计算结束索引，避免超出列表范围
                int endIndex = Math.min((currentPage-1)*pageSize + pageSize, stream.size());
                resultList = stream.subList((currentPage-1)*pageSize, endIndex);
            } else {
                // 如果原始列表不足 2 个元素，则返回空列表
                resultList = Collections.emptyList();
            }
            StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(resultList,stream.size());
            return RespBean.success("搜索成功",stSitinfoBDTO) ;
        }else {
            return  getRsvrRPage(7,1);
        }

    }

}
